﻿using AutoMapper;
using MediatR;
using Traceability.Common.Domain;
using Traceability.Common.Dtos;
using Traceability.Common.Infrastructure.InterFaces;
using Traceability.Common.Infrastructure.Repositories;
using Traceability.Enum;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Traceability.Bais.API.Appliction.Command.Roless
{
    /// <summary>
    /// 角色权限
    /// </summary>
    public class RolesPermissionHandler : IRequestHandler<RolesPermissionCommon, Result<List<RolePermissionDto>>>
    {
        private readonly IBaseRepository<Roles> rolesRep;
        private readonly IBaseRepository<Permission> permissionRep;
        private readonly IBaseRepository<RolesPermission> rolesPermissionRolesRep;
        private readonly IMapper mapper;

         /// <summary>
         ///  构造函数
         /// </summary>
         /// <param name="_rolesRep"></param>
         /// <param name="_permissionRep"></param>
         /// <param name="_rolesPermissionRolesRep"></param>
         /// <param name="mapper"></param>
        public RolesPermissionHandler(IBaseRepository<Roles> _rolesRep, IBaseRepository<Permission> _permissionRep, IBaseRepository<RolesPermission> _rolesPermissionRolesRep, IMapper mapper)
        {
            rolesRep = _rolesRep;
            permissionRep = _permissionRep;
            rolesPermissionRolesRep = _rolesPermissionRolesRep;
            this.mapper = mapper;
        }


        public async Task<Result<List<RolePermissionDto>>> Handle(RolesPermissionCommon request, CancellationToken cancellationToken)
        {
            var result = new Result<List<RolePermissionDto>>() { Code = (int)CommonErrorCode.search};
            try
            {
                var n = 1;
                var list = (from roless in rolesRep.GetAllS()
                            join rolespermission in rolesPermissionRolesRep.GetAllS()
                            on roless.RolesId equals rolespermission.RolesId
                            join permission in permissionRep.GetAllS()
                            on rolespermission.PermissionId equals permission.PermissionId
                            group permission by new { roless.RolesId, roless.RolesName, roless.CreatedTime,roless.Status} into g
                            select new RolePermissionDto
                            {
                                RolesId = g.Key.RolesId,
                                RolesName = g.Key.RolesName,
                                Status = g.Key.Status,
                                PermissionName = string.Join(",",g.Select(p=>p.PermissionName)),
                                CreatedTime = g.Key.CreatedTime ,
                                Number = n+1
                            }).ToList(); 
               if(!string.IsNullOrEmpty(request.permissionName))
                {
                   list = list.Where(c => c.PermissionName.Contains(request.permissionName)).ToList();
                }
                result.list = list.ToList();   
                result.Code = (int)CommonErrorCode.sucess;
                result.Msg = "查询成功";
            } 
            catch (Exception)
            {
                result.Msg = "查询失败";
                throw;
            }
            return result;
        }
    }
}
